﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Helpers="clr-namespace:Dispatcher.Helpers" 
    xmlns:Converters="clr-namespace:Dispatcher.Converters"
    xmlns:Repository="clr-namespace:Repository;assembly=Repository">

    <DataTemplate DataType = "{x:Type Repository:Person}">
        <Button  AllowDrop="True">
            
            <TextBlock Text="{Binding Path=FirstName}" ToolTip="{Binding Path=FirstName}"/>
        </Button>
    </DataTemplate>
    
    <Converters:LazyLoadConverter x:Key="LazyLoadConverter"/>
    
    <Converters:BirthdayToAgeConverter x:Key="BirthdayToAgeConverter"/>
    
    <Helpers:GridColumObservableCollection x:Key="DefaultColumnCollection">
        <DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
    </Helpers:GridColumObservableCollection>

    <Helpers:GridColumObservableCollection x:Key="PersonGroupColumnCollection">
        <DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Description" Binding="{Binding Description}"/>
        <DataGridTemplateColumn Header="Persons">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Person, Converter={StaticResource LazyLoadConverter}}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <Grid FocusManager.FocusedElement="{Binding ElementName=Element}">
                        <DataGrid Name ="Element" ItemsSource="{Binding Path=Person}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Persons">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Person, Converter={StaticResource LazyLoadConverter}}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <Grid FocusManager.FocusedElement="{Binding ElementName=Element}">
                        <DataGrid Name ="Element" ItemsSource="{Binding Path=Person}"/>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </Helpers:GridColumObservableCollection>

    <Helpers:GridColumObservableCollection x:Key="PersonColumnCollection">
        <DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True"/>
        <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
        <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/>
        <DataGridTextColumn Header="Patronymic" Binding="{Binding Patronymic}"/>
        <DataGridTextColumn Header="Phone" Binding="{Binding Phone}"/>
        <DataGridTemplateColumn Header="Birthday">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Birthday, StringFormat=dd.MM.yyyy}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <Grid FocusManager.FocusedElement="{Binding ElementName= BirthdayPicker}">
                        <DatePicker Name="BirthdayPicker" 
                                            SelectedDate="{Binding Path=Birthday, 
                                            ValidatesOnExceptions=true, 
                                            NotifyOnValidationError=true}" />
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Age">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Birthday, Converter={StaticResource BirthdayToAgeConverter}}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Groups">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=PersonGroup, Converter={StaticResource LazyLoadConverter}}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                        <DataGrid ItemsSource="{Binding Path=PersonGroup}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>     
        <DataGridTextColumn Header="Entity State" Binding="{Binding EntityState, Mode=OneWay}" IsReadOnly="True"/>
    </Helpers:GridColumObservableCollection>
</ResourceDictionary>
